<?php

namespace App\Http\Controllers\Wx;

use App\Http\Controllers\WxController;
use App\Models\Shop\Address;
use App\Models\Shop\Order;
use App\Models\Shop\Store;
use App\Models\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class UserController extends WxController
{
    public function getIndex()
    {
        $user = $this->user;
        $thumb = parse_url($user->thumb);
        if($thumb['scheme'] == 'http' || $thumb['scheme'] == 'https')
        {
        }else{
            $user->thumb = action('ImgController@getMember', ['uid' => $user->id, 'sourceName' => $user->thumb, 'size' => '100x100']);
        }
        $storeID = 1;
        if(isset($_COOKIE['storeID']) && is_numeric($_COOKIE['storeID']))
        {
            $storeID = $_COOKIE['storeID'];
        }
        //获取大学
        $store = Store::where('id', $storeID)->first();
        return view('wx.user')->with(['user' => $user, 'store' => $store]);
    }

    public function getEdit()
    {
        return view('wx.user-edit')->with(['user' => $this->user]);
    }
    public function postEdit(Request $request)
    {
        $InputData = $request->input();
        //验证
        $v = Validator::make($InputData, [
            'realName'      =>  'required|max:255'
        ]);

        if ($v->fails())
        {
            $backData = [
                'code'      =>  400,
                'msg'       =>  '请补全信息'
            ];
            return json_encode($backData);
        }
        $data = [
            'realName'      =>  $InputData['realName']
        ];
        $check = User::where('id', $this->user->id)->update($data);
        if ($check)
        {
            $backData = [
                'code'      =>  200,
                'msg'       =>  '修改成功',
                'url'       =>  action('Wx\UserController@getIndex')
            ];
        }else{
            $backData = [
                'code'      =>  400,
                'msg'       =>  '修改失败'
            ];
        }
        return json_encode($backData);
    }

    public function getAddress()
    {
        //获取地址
        $address = Address::where('uid', $this->user->id)->orderBy('id', 'desc')->get();
        return view('wx.user-address')->with(['address' => $address]);
    }
    public function getAddressAdd()
    {
        return view('wx.user-address-add');
    }
    public function postAddressAdd(Request $request)
    {
        $InputData = $request->input();
        //验证
        $v = Validator::make($InputData, [
            'name'          =>  'required|max:255',
            'phone'         =>  'required|',
            'address'       =>  'required|max:255',
        ]);

        if ($v->fails())
        {
            $backData = [
                'code'      =>  400,
                'msg'       =>  '请补全信息'
            ];
            return json_encode($backData);
        }
        //检测手机号
        if(!checkMobile($InputData['phone']))
        {
            $backData = [
                'code'      =>  400,
                'msg'       =>  '手机号不正确'
            ];
            return json_encode($backData);
        }

        $data = [
            'uid'       =>  $this->user->id,
            'name'      =>  $InputData['name'],
            'phone'     =>  $InputData['phone'],
            'address'   =>  $InputData['address']
        ];
        $check = Address::create($data);
        if ($check)
        {
            $backData = [
                'code'      =>  200,
                'msg'       =>  '添加成功',
                'url'       =>  action('Wx\UserController@getAddress')
            ];
        }else{
            $backData = [
                'code'      =>  400,
                'msg'       =>  '添加失败'
            ];
        }
        return json_encode($backData);
    }

    public function getAddressEdit($id)
    {
        //获取详情
        $address = Address::find($id);
        return view('wx.user-address-edit')->with(['address' => $address]);
    }
    public function postAddressEdit(Request $request, $id)
    {
        $InputData = $request->input();
        //验证
        $v = Validator::make($InputData, [
            'name'          =>  'required|max:255',
            'phone'         =>  'required|',
            'address'       =>  'required|max:255',
        ]);

        if ($v->fails())
        {
            $backData = [
                'code'      =>  400,
                'msg'       =>  '请补全信息'
            ];
            return json_encode($backData);
        }
        //检测手机号
        if(!checkMobile($InputData['phone']))
        {
            $backData = [
                'code'      =>  400,
                'msg'       =>  '手机号不正确'
            ];
            return json_encode($backData);
        }

        $data = [
            'name'      =>  $InputData['name'],
            'phone'     =>  $InputData['phone'],
            'address'   =>  $InputData['address']
        ];
        $check = Address::where('id', $id)->update($data);
        if ($check)
        {
            $backData = [
                'code'      =>  200,
                'msg'       =>  '更新成功',
                'url'       =>  action('Wx\UserController@getAddress')
            ];
        }else{
            $backData = [
                'code'      =>  400,
                'msg'       =>  '修改失败'
            ];
        }
        return json_encode($backData);
    }


    /**
     * 获取地址列表
     * @return string
     */
    public function getAddressList()
    {
        $address = Address::where('uid', $this->user->id)->orderBy('id', 'desc')->get();
        /*
         * 姓名：魏磐；电话：18291470233；地址：陕西省西安市鸿基新城9号楼1单円0902号fdhgfhewr23434234；邮编：725401'
         */
        $backData = [];
        foreach ($address as $v)
        {
            $backData[] = '氏名：'.$v->name.'；電話：'.$v->phone.'；住所：'.$v->address;
        }
        return json_encode($backData);
    }

    /**
     * 订单页
     * @return $this
     */
    public function getOrder()
    {
        $order = Order::where('uid', $this->user->id)->take(10)->orderBy('id', 'desc')->get();
        return view('wx.user-order')->with(['order' => $order]);
    }
    public function postOrder(Request $request)
    {
        $page = 0;
        if($request->has('page'))
        {
            $page = $request->input('page');
            $order = Order::where('uid', $this->user->id)->skip($page*10)->take(10)->get();
            if(count($order) > 1)
            {
                $html = '';
                foreach($order as $v)
                {
                    $html .= '<li>';
                    $html .= '<a href="" class="item-link item-content">';
                    $html .= '<div class="item-inner"><div class="item-title-row closeLink">';
                    $html .= '<div class="item-title">'.$v->orderNumber.'</div>';
                    $html .= '<div class="item-after">'.$v->amount.' 円</div></div>';
                    $html .= '<div class="item-subtitle">';
                    if($v->payState == 0)
                    {
                        $html .= '<span class="label label-danger labelTags">未付款</span>';
                    }else{
                        $html .= '<span class="label label-success labelTags">已付款</span>';
                    }
                    if($v->logisticsState == 0)
                    {
                        $html .= '<span class="label label-danger labelTags">未接单</span>';
                    }elseif($v->logisticsState == 1){
                        $html .= '<span class="label label-info labelTags">配送中</span>';
                    }elseif($v->logisticsState == 2){
                        $html .= '<span class="label label-success labelTags">已送达</span>';
                    }
                    $html .= '<span>'.$v->created_at.'</span></div>';
                    $html .= '<div class="item-text">'.$v->orderList.'</div></div></a></li>';
                }

                $backData = [
                    'code'      =>  0,
                    'msg'       =>  'success',
                    'data'      =>  $html
                ];
                return json_encode($backData);
            }
        }
        $backData = [
            'code'      =>  1,
            'msg'       =>  'error'
        ];
        return json_encode($backData);
    }

    public function getOrderShow($id)
    {
        $order = Order::where('uid', $this->user->id)->where('id',$id)->first();
        return view('wx.user-order-show')->with(['order' => $order]);
    }
}
